package cn.com.zcode.dic.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import cn.com.zcode.dic.model.Dictionary;

public class ApplicationCache {
	private Logger logger = Logger.getLogger(ApplicationCache.class);
	private final static ApplicationCache cache = new ApplicationCache();
	public static Map<String, Dictionary> dictionarys = new ConcurrentHashMap<String, Dictionary>();
	public static Map<String, List<Dictionary>> dictionaryTypes = new ConcurrentHashMap<String, List<Dictionary>>();

	public static ApplicationCache getInstance() {
		return cache;
	}

	private ApplicationCache() {
		super();
	}

	public void addDictionary(Dictionary dictionary) {
		logger.info("ADD DICTIONARY :" + dictionary);
		dictionarys.put(dictionary.getCode(), dictionary);
		Dictionary parent = dictionary.getParent();
		List<Dictionary> list = null;
		String key = parent != null ? parent.getCode() : "";

		list = dictionaryTypes.get(key);

		if (list == null) {
			list = new ArrayList<Dictionary>();

		}
		dictionaryTypes.put(key, list);
		list.add(dictionary);

	}

	public static Dictionary getDictionaryByCode(String code) {
		return dictionarys.get(code);
	}

	public static List<Dictionary> getDictionaryByType(String type) {
		if (type == null) {
			type = "";
		}
		return dictionaryTypes.get(type);
	}

}
